package com.example.farmer.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.farmer.Entity.Order;

public class BuyDatabase extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "farmer.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_ORDERS = "orders";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_PRODUCT_PRICE = "product_price";
    private static final String COLUMN_QUANTITY = "quantity";
    private static final String COLUMN_FREIGHT = "freight";
    private static final String COLUMN_TOTAL = "total";
    private static final String COLUMN_ADDRESS = "address";
    private static final String COLUMN_PAYMENT_STATUS = "payment_status";
    private static final String COLUMN_PRODUCT_NAME = "product_name";
    private static final String COLUMN_METHOD = "method";
    private static final String COLUMN_IMAGE_URI = "product_image";

    public BuyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_ORDERS_TABLE = "CREATE TABLE " + TABLE_ORDERS + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_PRODUCT_PRICE + " REAL,"
                + COLUMN_QUANTITY + " INTEGER,"
                + COLUMN_FREIGHT + " REAL,"
                + COLUMN_TOTAL + " REAL,"
                + COLUMN_ADDRESS + " TEXT,"
                + COLUMN_PAYMENT_STATUS + " TEXT,"
                + COLUMN_PRODUCT_NAME + " TEXT,"
                + COLUMN_METHOD + " TEXT,"
                + COLUMN_IMAGE_URI + " TEXT"
                + ")";
        db.execSQL(CREATE_ORDERS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDERS);
        onCreate(db);
    }

    public void insertOrder(Order order) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCT_PRICE, order.getProductPrice());
        values.put(COLUMN_QUANTITY, order.getQuantity());
        values.put(COLUMN_FREIGHT, order.getFreight());
        values.put(COLUMN_TOTAL, order.getTotal());
        values.put(COLUMN_ADDRESS, order.getAddress());
        values.put(COLUMN_PAYMENT_STATUS, order.getPaymentStatus());
        values.put(COLUMN_PRODUCT_NAME, order.getProductName());
        values.put(COLUMN_METHOD, order.getMethod());
        values.put(COLUMN_IMAGE_URI, order.getProductImage());
        db.insert(TABLE_ORDERS, null, values);
        db.close();
    }
}
